home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 13 / develop 13 code / DeviceLoopInDrag / OurMain.cp < prev    next >
Encoding:
Text File  |  1992-10-30  |  8.3 KB  |  327 lines  |  [TEXT/MPS ]

  1. // OurMain.cp
  2. // The derived application class.
  3. // The TApplication and TDocument folders have been left unchanged.
  4. // The application is tailored in the functions below.
  5.  
  6. #include "OurMain.h"
  7. #include "OurMainShared.h"
  8.  
  9. #include <Dialogs.h>
  10. #include <Memory.h>
  11. #include <Menus.h>
  12. #include <Resources.h>
  13.  
  14. TOurApp*    gOurApp;    // Our global application object.
  15.  
  16. // ---------------------------------------------------------------------
  17. // main
  18. // Application entry point.
  19. // It all starts here.
  20. int
  21. main()
  22. {
  23.     gOurApp = new TOurApp;
  24.     if(!gOurApp)
  25.     {
  26.         return 0;
  27.     }
  28.     gOurApp->InitOurApp();
  29.     gOurApp->EventLoop();
  30.     return 0;
  31. }
  32.  
  33. // ---------------------------------------------------------------------
  34. // TOurApp::DoAbout
  35. // Display the "About…" box.
  36. void
  37. TOurApp::DoAbout()
  38. {
  39.     DialogPtr    pDlog;
  40.     Str255        versString;
  41.     short        itemType;
  42.     Handle        hItem;
  43.     Rect        box;
  44.     short        itemHit;
  45.     GrafPtr        savePort;
  46.             // Get dialog.
  47.     pDlog = GetNewDialog(rAboutDlog, kDefaultStorage, (WindowPtr) kInFrontOfAll);
  48.             // Set version informaton.
  49.     GetDItem(pDlog, iAboutTitle, &itemType, &hItem, &box);
  50.     this->GetVersion(versString);
  51.     SetIText(hItem, versString);
  52.             // Add default outline around OK button.
  53.     ShowWindow(pDlog);
  54.     GetDItem(pDlog, iAboutOk, &itemType, &hItem, &box);
  55.     GetPort(&savePort);
  56.     SetPort(pDlog);
  57.     PenSize(3,3);
  58.     InsetRect(&box, -4, -4);
  59.     FrameRoundRect(&box, 16, 16);
  60.     PenNormal();
  61.     SetPort(savePort);
  62.             // Wait for user action.
  63.     ModalDialog(kNoFilterProc, &itemHit);
  64.             // Clean up.
  65.     DisposDialog(pDlog);
  66.     HiliteMenu(0);
  67. }
  68.  
  69. // ------------------------------------------------------------------------
  70. // TOurMain::DoMenuCommand
  71. // This is called when an item is chosen from the menu bar (after calling
  72. // MenuSelect or MenuKey). It does the right thing for each command.
  73. // 18-Mar-92 Remove file menu.
  74. void
  75. TOurApp::DoMenuCommand(short menuID, short menuItem)        //  override
  76. {
  77.  
  78.     Str255        daName;
  79.     short        daRefNum;
  80.  
  81.     switch (menuID)
  82.     {
  83.         case mApple:
  84.             switch ( menuItem )
  85.             {
  86.                 case iAbout:// bring up alert for About
  87.                     this->DoAbout();
  88.                     break;
  89.                 default:    // all non-About items in this menu are DAs et al 
  90.                     GetItem(GetMHandle(mApple), menuItem, daName);
  91.                     daRefNum = OpenDeskAcc(daName);
  92.                     HiliteMenu(0);
  93.                     break;
  94.             } // switch
  95.             break;
  96.         case mFile:
  97.             switch ( menuItem )
  98.             {
  99.                 case iQuit:
  100.                     this->ExitLoop();
  101.                     break;
  102.                 default:
  103.                     break;
  104.             } // switch
  105.             break;
  106.         default:
  107.             break;
  108.     } // switch
  109. } // DoMenuCommand
  110.  
  111. // ------------------------------------------------------------------------
  112. // TOurApp::GetVersion
  113. // Get the current version of the application.
  114. // Get long version string from 'vers' resource #1
  115. //
  116. void
  117. TOurApp::GetVersion(Str255 versStr)
  118. {
  119.     Handle    hRes;
  120.     char    *pRes;
  121.     short    i;
  122.  
  123.     if (hRes = GetResource('vers', 1))
  124.     {
  125.         pRes = *hRes;
  126.         pRes += 7 + pRes[6];        // long version pstring
  127.         for(i=0; i<=pRes[0]; i++)
  128.             versStr[i] = pRes[i];    // copy version pstring
  129.     }
  130. }
  131.  
  132. // ---------------------------------------------------------------------
  133. // TOurApp::InitOurApp
  134. // Do the things that our derived application class requires.
  135. void
  136. TOurApp::InitOurApp()
  137. {
  138.     SysEnvRec envRec;
  139.     (void) SysEnvirons(curSysEnvVers, &envRec);
  140.             // System 7 is required
  141.     if(envRec.systemVersion<0x0700)
  142.     {
  143.         this->BigBadError(kUserStrID, kErrNotSevenOh);
  144.     }
  145.             // Install menus.
  146.     Handle menuBar = GetNewMBar(rMenuBar);
  147.     SetMenuBar(menuBar);        // Copy to current menu list.
  148.     DisposHandle(menuBar);        // Don't need it anymore.
  149.             // Add DA names to Apple menu.
  150.     AddResMenu(GetMHandle(mApple), 'DRVR');
  151.     DrawMenuBar();
  152.             // Create a document, window, and window object.
  153.     this->fDocList->AddDoc((TDocument*) new TOurDoc(kWindResID));
  154.  
  155. }
  156.  
  157. // ------------------------------------------------------------------------
  158. // TOurDoc::TOurDoc
  159. // Document object constructor.
  160. // The base class creates the Window Manager window pointer.
  161. // We create the window object here.
  162. // The resID is used by the base class constructor.
  163. TOurDoc::TOurDoc(short resID) : TDocument(resID)
  164. {
  165.     this->fWinObj = new TWin;
  166. }
  167.  
  168. // ------------------------------------------------------------------------
  169. // TOurDoc::~TOurDoc
  170. // Document object destructor.
  171. // Delete the window object.
  172. TOurDoc::~TOurDoc()
  173. {
  174.     delete this->fWinObj;
  175. }
  176.  
  177. // ------------------------------------------------------------------------
  178. // TOurDoc::DoUpdate
  179. // Document object.
  180. // Entry for update event action.
  181. void
  182. TOurDoc::DoUpdate()
  183. {
  184.     BeginUpdate(this->fDocWindow);
  185.     this->fWinObj->Draw();
  186.     EndUpdate(this->fDocWindow);
  187. }
  188.  
  189. // ------------------------------------------------------------------------
  190. // TWin::TWin
  191. // Window object constructor.
  192. // Creates the window contents (TArt objects).
  193. TWin::TWin()
  194. {
  195.     this->fArt1 = new TArt;
  196.     this->fArt1->Init(kArt1ID, kArt1LocV, kArt1LocH);
  197.     this->fArt2 = new TArt;
  198.     this->fArt2->Init(kArt2ID, kArt2LocV, kArt2LocH);
  199.     this->fArt3 = new TArt;
  200.     this->fArt3->Init(kArt3ID, kArt3LocV, kArt3LocH);
  201. }
  202.  
  203. // ------------------------------------------------------------------------
  204. // TWin::~TWin
  205. // Window object destructor.
  206. // Deletes the window contents (TArt objects).
  207. TWin::~TWin()
  208. {
  209.     delete this->fArt1;
  210.     delete this->fArt2;
  211.     delete this->fArt3;
  212. }
  213.  
  214. // ------------------------------------------------------------------------
  215. // TWin::Draw
  216. // Window object.
  217. // Within BeginUpdate/EndUpdate.
  218. void
  219. TWin::Draw()
  220. {
  221.             // Setup and do DeviceLoop drawing.
  222.             // Pass the Window object in userData.
  223.     long            userData=(long)this;
  224.     DeviceLoopFlags    flags=0;
  225.     GrafPtr            myPort;
  226.     GetPort(&myPort);
  227.     DeviceLoop(myPort->visRgn, TWin::DrawProc, userData, flags);
  228. };
  229.  
  230. // ------------------------------------------------------------------------
  231. // TWin::DrawProc
  232. // Called by DeviceLoop.
  233. // A static function.  Must be in a resident segment, locked and unpurgeable.
  234. // Because it's static, it cannot access object member variables directly.
  235. // We use the window object passed in userData to access its variables.
  236. #pragma segment Main
  237. pascal void
  238. TWin::DrawProc(short depth, short /*deviceFlags*/,
  239.                                         GDHandle hTargetDevice,
  240.                                         long userData)
  241. {
  242.             // Get the window object from userData.
  243.     TWin* theWinObject = (TWin*) userData;
  244.             // Use depth of 1 if we have a computer without CQD.
  245.     depth = (hTargetDevice==NULL)?1:depth;
  246.             // Draw our objects.
  247.     theWinObject->fArt1->Draw(depth);
  248.     theWinObject->fArt2->Draw(depth);
  249.     theWinObject->fArt3->Draw(depth);
  250. };
  251.  
  252. // ------------------------------------------------------------------------
  253. // TArt::Draw
  254. // All art objects (PICT's) are drawn from here.
  255. // This is where we distinguish between B&W and color renderings
  256. // of TArt objects.  The B&W rendering has a resource ID that
  257. // is kBWOffset larger than its color counterpart.
  258. // If the function can't find one version, it will try the other.
  259. void
  260. TArt::Draw(short depth)
  261. {
  262.             // Don't draw empty art.
  263.     if(this->fPictID==0)
  264.         return;
  265.     PicHandle    hPict;
  266.     if(depth<8)
  267.     {
  268.             // Use B&W PICT.
  269.         hPict = (PicHandle) GetResource('PICT', this->fPictID+kBWOffset);
  270.         if(!hPict)
  271.         {
  272.                 // No B&W PICT, try color (may really be B&W).
  273.             hPict = (PicHandle) GetResource('PICT', this->fPictID);
  274.         }
  275.     }
  276.     else
  277.     {
  278.             // Use color PICT.
  279.         hPict = (PicHandle) GetResource('PICT', this->fPictID);
  280.         if(!hPict)
  281.         {
  282.                 // No color PICT, try B&W.
  283.             hPict = (PicHandle) GetResource('PICT', this->fPictID+kBWOffset);
  284.         }
  285.     }
  286.     if(hPict)
  287.     {
  288.             // We could have used the fDrawRect private member variable,
  289.             // but the article used the access function so we'll stick
  290.             // with that.
  291.         Rect theDrawRect;
  292.         this->GetDrawRect(theDrawRect);
  293.         HLock((Handle) hPict);
  294.         DrawPicture(hPict, &theDrawRect);
  295.         HUnlock((Handle) hPict);
  296.     }
  297. };
  298.  
  299. // ------------------------------------------------------------------------
  300. void
  301. TArt::GetDrawRect(Rect& theRect)
  302. {
  303.     theRect = this->fDrawRect;
  304. }
  305.  
  306. // ------------------------------------------------------------------------
  307. // TArt::Init
  308. // Initialize the art object.
  309. // Set it's PICT ID and drawing location.
  310. void
  311. TArt::Init(short resID, short drawLocV, short drawLocH)
  312. {
  313.     this->fPictID = resID;
  314.     SetRect(&this->fDrawRect, 0, 0, 0, 0);
  315.         // Normalize PICT to desired drawing location.
  316.     PicHandle hPict = (PicHandle) GetResource('PICT', this->fPictID);
  317.     if(hPict)
  318.     {
  319.                 // Get the original drawing rect.
  320.         this->fDrawRect = (**hPict).picFrame;
  321.                 // Normalize rect to 0,0 in case the artist didn't.
  322.         OffsetRect(&this->fDrawRect, -this->fDrawRect.left,
  323.                                         -this->fDrawRect.top);
  324.                 // Move rect to loc.
  325.         OffsetRect(&this->fDrawRect, drawLocH, drawLocV);
  326.     }
  327. };